package com.client.job.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 将对象中的属性值转成集合
 *
 * @author monxz
 * @Date 2020年12月25日上午11:10:23
 * @Des
 *
 */
public class BeanReflectUtils {

	public static void main(String[] args) {

	}

	public static Map<String, Object> beanProToListKv(Object obj)
			throws IllegalArgumentException, IllegalAccessException {
		Map<String, Object> map = new HashMap<String, Object>();
		Field[] fathers = obj.getClass().getSuperclass().getDeclaredFields();
		Field[] fields = obj.getClass().getDeclaredFields();
		for (Field field : fields) {
			field.setAccessible(true);
			if (field.get(obj) != null) {
				map.put(field.getName(), field.get(obj));
			}
		}
		for (Field field : fathers) {
			field.setAccessible(true);
			if (field.get(obj) != null) {
				map.put(field.getName(), field.get(obj));
			}
		}

		return map;
	}

	/**
	 * 将对象中的属性值转成集合
	 * 
	 * @param t
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	public static List<Object> beanProToList(Object obj) throws IllegalArgumentException, IllegalAccessException {
		List<Object> list = new ArrayList<>();
		Field[] fields = obj.getClass().getDeclaredFields();
		for (Field field : fields) {
			field.setAccessible(true);
			list.add(field.get(obj));
		}
		return list;
	}

	/**
	 * 将对象中的属性值转成集合
	 * 
	 * @param t
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	public static List<List<Object>> beanProToList(List<?> objList)
			throws IllegalArgumentException, IllegalAccessException {
		if (objList == null || objList.isEmpty()) {
			return null;
		}
		Field[] fields = objList.get(0).getClass().getDeclaredFields();
		List<List<Object>> res = new ArrayList<>();
		for (Object obj : objList) {
			List<Object> list = new ArrayList<>();
			for (Field field : fields) {
				field.setAccessible(true);
				list.add(field.get(obj));
			}
			res.add(list);
		}

		return res;
	}

	/**
	 * 将list转成bean集合
	 * 
	 * @param <T>
	 * @param dataList
	 * @param t
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static <T> List<T> ListToBeans(List<List<String>> dataList, T t) throws Exception {
		List<T> res = new ArrayList<>();
		Field[] fields = t.getClass().getDeclaredFields();
		for (List<String> strList : dataList) {
			T newT = (T) t.getClass().newInstance();
			for (int i = 0; i < fields.length; i++) {
				fields[i].setAccessible(true);
				String type = fields[i].getGenericType().toString();
				if (type.contains("String")) {
					fields[i].set(newT, strList.get(i));
				} else if (type.contains("Integer") || type.contains("int")) {
					fields[i].set(newT, Integer.valueOf(strList.get(i)));
				} else if (type.contains("Double") || type.contains("double")) {
					fields[i].set(newT, Double.valueOf(strList.get(i)));
				} else {
					fields[i].set(newT, null);
				}
			}
			res.add(newT);
		}
		return res;
	}

	/**
	 * 将list转成bean集合
	 * 
	 * @param <T>
	 * @param dataList
	 * @param t
	 * @return
	 * @return
	 * @throws Exception
	 */
	public static <T> Object ListToBean(List<String> dataList, T t) throws Exception {
		Field[] fields = t.getClass().getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			fields[i].setAccessible(true);
			String type = fields[i].getGenericType().toString();
			if (type.contains("String")) {
				fields[i].set(t, dataList.get(i));
			} else if (type.contains("Integer") || type.contains("int")) {
				fields[i].set(t, Integer.valueOf(dataList.get(i)));
			} else if (type.contains("Double") || type.contains("double")) {
				fields[i].set(t, Double.valueOf(dataList.get(i)));
			} else {
				fields[i].set(t, null);
			}
		}
		return t;
	}

}
